package cn.hchaojie.javase.day11;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.Test;

import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;

import cn.hchaojie.javase.week2.Score;

public class TestScore {
	// 查找某个学生的所有成绩
	@Test
	public void test() throws IOException {
		List<String> list = Files.readAllLines(Paths.get("data/scores1.csv"));

		Map<String, List<Score>> scores = new HashMap<>();

		for (String line : list) {
			String[] data = line.split(",");

			String name = data[0];
			String course = data[1];
			double score = Double.parseDouble(data[2]);

			// 当前遍历的学生名字在map里面有或者没有
			if (scores.containsKey(name)) {
				// 取出分数记录的List集合
				List<Score> scoreByStudent = scores.get(name);

				// 往集合里面添加新的分数

				scoreByStudent.add(new Score(score, name, course));
			} else {
				// 没有这个学生的分数记录
				List<Score> scoreByStudent = new ArrayList<>(); // 分数记录

				scoreByStudent.add(new Score(score, name, course));

				// 存分数记录
				scores.put(name, scoreByStudent);
			}
		}

		// for (Entry<String, List<Score>> entry : scores.entrySet()) {
		// System.out.println("学生：" + entry.getKey());
		//
		// System.out.println(entry.getValue());
		// }

		List<Score> list2 = scores.get("张三");

		System.out.println(scores.get("张三"));

		// Map<String, List<Score>>
	}

	// 查找某个学生、某门课的成绩
	@Test
	public void test2() throws IOException {
		List<String> list = Files.readAllLines(Paths.get("data/scores1.csv"));

		Map<String, Map<String, Score>> scores = new HashMap<>();

		for (String line : list) {
			String[] data = line.split(",");

			String name = data[0];
			String course = data[1];
			double score = Double.parseDouble(data[2]);

			// 当前遍历的学生名字在map里面有或者没有
			if (scores.containsKey(name)) {
				scores.get(name).put(course, new Score(score, name, course));
			} else {
				Map<String, Score> scoreByCourse = new HashMap<>();
				scoreByCourse.put(course, new Score(score, name, course));
				
				scores.put(name, scoreByCourse);
			}
		}

//		for (Entry<String, Map<String, Score>> entry : scores.entrySet()) {
//			System.out.println("学生：" + entry.getKey());
//			System.out.println(entry.getValue());
//		}
		
		System.out.println(scores.get("张三").get("语文"));
	}
}
